/**
 * 获取美食分类
 * 
 * 
 * 
 * 
 */


const Crawler = require("crawler");

const unirest = require('unirest');






var c = new Crawler({
  maxConnections: 10,
  jQuery: true,
  // This will be called for each crawled page
  callback: function (error, res, done) {
      if (error) {
          console.log(error);
      } else {
          console.log(res.statusCode);
          //console.log(res.body);
          if (res.statusCode === 200) {
              console.log('callback');
              var $ = res.$;

              let data = $('#J-shopall > div > div:nth-child(3) > dl:nth-child(3) > dd > ul').text().replace(/(\r\n|\n|\r|\t|\t\n|\n\t)/gm,"");


              let foodCategories = []


              $('#J-shopall > div > div:nth-child(3) > dl:nth-child(3) > dd > ul').children('li').children('a').contents().each((i, element) => { 
                
                console.log($(element.parent).attr('href'))


                


                console.log(element.data)

                foodCategories.push({
                  categoryName: element.data.replace(/(\r\n|\n|\r|\t|\t\n|\n\t)/gm,""),
                  link:$(element.parent).attr('href')
                })
              }); 

  
              

              console.log(foodCategories)

              if (foodCategories && foodCategories.length > 0) {
                for (const foodCategry of foodCategories) {                  
                  unirest.post('http://127.0.0.1:7001/foodcategory')
                  .headers({'Accept': 'application/json', 'Content-Type': 'application/json'})
                  .send(foodCategry)
                  .end(function (response) {
                      console.log(response.body);
                  });  
                }
              }
            
              

          }
      }
      done();
  }
});



c.queue('http://www.dianping.com/shopall/4/0');

